import Vue from 'vue'
// element-ui
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import 'normalize.css/normalize.css'// A modern alternative to CSS resets
import '@/assets/styles/common.css'
import '@/assets/styles/index.less'// custome global css

import App from './App'
import router from './router'
import store from './store'
import mixins from '@/mixins'
// echarts
import echarts from 'echarts';
Vue.prototype.$echarts = echarts
// 全局定义echarts
import ECharts from 'vue-echarts'
import 'echarts/lib/chart/bar'
import 'echarts/lib/component/tooltip'
Vue.component('v-chart', ECharts)
// mock
import '@/mock/'
// avue
Vue.use(window.AVUE, {
  size: 'mini'
});

// enable element zh-cn
Vue.use(ElementUI, { zhLocale })

// register global mixins.
Vue.mixin(mixins)

// 分页的全局size配置;
Vue.prototype.$pageSizeAll = [10, 50, 100, 200, 500]

Vue.config.productionTip = false

//定义全局变量
Vue.prototype.$eventbus = new Vue()

let instance = null
if (window.__POWERED_BY_QIANKUN__) {
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
}

function render() {
  instance = new Vue({
    router,
    store,
    render: (h) => h(App)
  }).$mount('#app')
}

export async function bootstrap() {
  //console.log('VueMicroApp bootstraped')
}
export async function mount(props) {
  // console.log('VueMicroApp mount', props)
  const storeData = window.store.getStore()
  window.store.update({
      ...storeData,
      c: 11,
  })

  window.custom.emit('test', {
    a: 1
  })

  Vue.prototype.$qiankunActions = props
  render()
  // 从生命周期 mount 中获取通信方法，使用方式和 master 一致
  props.onGlobalStateChange((state, prev) => {
    // console.log('子应用onGlobalStateChange', state, prev)
    if (state.type === 'postUserInfoSuccess') {
      instance.$store.commit('user/changeState', state.value)
    }
    if (state.type === 'globalRequest') {
      window.globalRequest = state.value
      // console.log(window.globalRequest, 'window.globalRequest')
    }
    if (state.type === 'globalConfig') {
      window.globalConfig = state.value
    }
  })
}
export async function unmount() {
  //console.log('VueMicroApp unmount')
  instance.$destroy()
  instance = null
}

if (!window.__POWERED_BY_QIANKUN__) {
  render()
}

